import Vue from 'vue'
import Router from 'vue-router'
import login from '@/views/login'
import home from '@/views/home'

Vue.use(Router)

let router =  new Router({
  routes: [
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path:'/home',
        name:'home',
        component:home
    },
    {
        path:'/',
        redirect:'/home'
    }
  ]
})

// 全局守卫

let arr = ['home'];

router.beforeEach((to,from,next) => {
    if(arr.indexOf(to.name) != -1){
        if(localStorage.getItem('name')){
            next()
        }else{
            next('/login')
        }
    }else{
        next()
    }
})

export default router;


